const db = require('../db/index');

// 产品入库 创建产品
exports.createProduct = (req, res) => {
  let {
    product_id,
    product_name,
    product_category,
    product_unit,
    product_inwarehouse_number,
    product_single_price,
    product_create_person,
    in_memo
  } = req.body;
  const product_create_time = new Date()
  const product_all_price = product_inwarehouse_number * 1 * product_single_price * 1;
  let sql0 = 'select * from product where product_id=?';
  db.query(sql0, product_id, (err, results) => {
    if (err) return res.cc(err);
    if (results.length > 0) {
      res.send({
        status: 1,
        message: '产品编号已存在'
      })
    }
    let sql = 'insert into product set ?';
    db.query(sql, {
      product_id,
      product_name,
      product_category,
      product_unit,
      product_inwarehouse_number,
      product_single_price,
      product_create_person,
      product_all_price,
      product_create_time,
      in_memo,
    }, (err, results) => {
      if (err) return res.cc(err);
      res.send({
        status: 0,
        message: '添加产品成功'
      })
    })
  })

}

// 删除产品信息
exports.deleteProduct = (req, res) => {
  let sql = 'delete from product where id=?';
  db.query(sql, req.body.id, (err, results) => {
    if (err) return res.cc(err)
    if (results.affectedRows !== 1) return res.cc('删除产品失败')
    res.send({
      status: 0,
      message: '删除产品成功'
    })
  })
}

// 修改产品信息
exports.editProduct = (req, res) => {
  let {
    id,
    product_name,
    product_category,
    product_unit,
    product_inwarehouse_number,
    product_single_price,
    in_memo
  } = req.body;
  const product_all_price = product_inwarehouse_number * 1 * product_single_price * 1;
  const product_update_time = new Date();
  let sql = 'update product set product_name=?,product_category=?,product_unit=?,product_inwarehouse_number=?,product_single_price=?,product_all_price=?,in_memo=?,product_update_time=? where id=?';
  db.query(sql, [
    product_name,
    product_category,
    product_unit,
    product_inwarehouse_number,
    product_single_price,
    product_all_price,
    in_memo,
    product_update_time,
    id
  ], (err, results) => {
    if (err) return res.cc(err);
    res.send({
      status: 0,
      message: '修改产品成功'
    })
  })
}

// 获取产品列表
exports.getProductList = (req, res) => {
  // product_inwarehouse_number >=0  大于等于0
  let sql = 'select * from product where product_inwarehouse_number >=0';
  db.query(sql, (err, results) => {
    if (err) return res.cc(err);
    res.send({
      status: 0,
      message: '获取产品列表成功',
      results
    })
  })
}

// 产品申请出库
exports.applyProductOut = (req, res) => {
  let {
    id,
    product_out_id,
    product_out_number,
    product_single_price,
    product_out_apply_person,
    product_memo
  } = req.body;
  let product_out_status = '申请出库';
  let product_out_price = product_out_number * 1 * product_single_price * 1;
  let product_apply_time = new Date();
  let sql0 = 'select * from product where product_out_id=?';
  db.query(sql0, product_out_id, (err, results) => {
    if (err) return res.cc(err);
    if (results.length > 0) {
      res.send({
        status: 1,
        message: '出库编号已存在'
      })
    }
    let sql = 'update product set product_out_id=?,product_out_number=?,product_single_price=?,product_out_apply_person=?,product_memo=?,product_out_status=?,product_out_price=?,product_apply_time=? where id=?';
    db.query(sql, [
      product_out_id,
      product_out_number,
      product_single_price,
      product_out_apply_person,
      product_memo,
      product_out_status,
      product_out_price,
      product_apply_time,
      id
    ], (err, results) => {
      if (err) return res.cc(err);
      if (results.affectedRows !== 1) return res.cc('申请出库失败');
      res.send({
        status: 0,
        message: '产品申请出库成功'
      })
    })

  })
}

// 产品审核列表  product_out_status 同意  否决
exports.getApplyProductList = (req, res) => {
  let sql = 'select * from product where product_out_status not in ("同意")';
  db.query(sql, (err, results) => {
    if (err) return res.cc(err)
    res.send({
      status: 0,
      message: '获取产品审核列表成功',
      results
    })
  })
}

// 对产品进行撤回申请
exports.withDrawApplyProduct = (req, res) => {
  let sql = 'update product set product_out_id=NULL,product_out_status=NULL,product_out_number=NULL,product_out_apply_person=NULL,product_memo=NULL,product_apply_time=NULL where id=?';
  db.query(sql, req.body.id, (err, results) => {
    if (err) return res.cc(err)
    if (results.affectedRows !== 1) return res.cc('撤回产品申请失败')
    res.send({
      status: 0,
      message: '撤回产品申请成功'
    })
  })
}

// 产品审核
exports.auditProduct = (req, res) => {
  let {
    id,
    product_out_id,
    product_out_status,
    audit_memo,
    product_out_price,
    product_out_audit_person,
    product_out_apply_person,
    product_inwarehouse_number,
    product_single_price,
    product_out_number
  } = req.body;
  const product_audit_time = new Date();
  if (product_out_status === '同意') {
    // 出库的价格
    product_out_price = product_out_number * 1 * product_single_price * 1;
    // 新的库存数量
    const newWarehouseNumber = product_inwarehouse_number * 1 - product_out_number * 1;
    // 新的库存总价
    const product_all_price = newWarehouseNumber * 1 * product_single_price * 1;
    let sql = 'insert into outproduct set ?';
    db.query(sql, {
      product_out_id,
      product_out_number,
      product_out_price,
      product_out_apply_person,
      product_audit_time,
      product_out_audit_person,
      audit_memo
    }, (err, results) => {
      if (err) return res.cc(err);
      let sql1 = 'update product set product_inwarehouse_number=?,product_all_price=?,product_out_audit_person=?,product_audit_time=?, product_out_id=NULL,product_single_price=NULL,product_out_status=NULL,product_out_number=NULL,product_out_apply_person=NULL,product_memo=NULL,product_apply_time=NULL where id=?';
      db.query(sql1, [newWarehouseNumber, product_all_price, product_out_audit_person, product_audit_time, id], (err, results) => {
        if (err) return res.cc(err);
        res.send({
          status: 0,
          message: '产品审核成功'
        })
      })
    })
  }
  if (product_out_status === '否决') {
    let sql = 'update product set audit_memo=?,product_out_status=?product_audit_time=?,product_out_audit_person=?, where id=?';
    db.query(sql, [audit_memo, product_out_status, product_audit_time, product_out_audit_person, id], (err, results) => {
      if (err) return res.cc(err);
      res.send({
        status: 0,
        message: '产品被否决'
      })
    })
  }
}

// 通过入库编号进行搜索
exports.searchProductByInWareId = (req, res) => {
  let sql = 'select * from product where product_id = ?';
  db.query(sql, req.query.product_id, (err, results) => {
    if (err) return res.cc(err)
    res.send({
      status: 0,
      results
    })
  })
}

// 通过申请编号进行搜索
exports.searchApplyProductByInWareId = (req, res) => {
  let sql = 'select * from product where product_out_id=?';
  db.query(sql, req.query.product_out_id, (err, results) => {
    if (err) return res.cc(err)
    res.send({
      status: 0,
      results
    })
  })
}

// 通过出库编号进行搜索
exports.searchProductByOutWareId = (req, res) => {
  let sql = 'select * from outproduct where product_out_id=?';
  db.query(sql, req.query.product_out_id, (err, results) => {
    if (err) return res.cc(err)
    res.send({
      status: 0,
      results
    })
  })
}

// 获取产品总数
exports.getAllProductCount = (req, res) => {
  let sql = 'select * from product where product_out_status="" || isNull(product_out_status)';
  db.query(sql, (err, results) => {
    if (err) return res.cc(err)
    res.send({
      status: 0,
      message: '获取产品总数成功',
      total: results.length
    })
  })
}

// 获取申请出库产品总数
exports.getAllApplyProductCount = (req, res) => {
  // OR ===  ||    AND === &&
  // <>  不等于  not in ("NULL") 不等于null  not in ("NULL") AND product_out_status <>"" 不等于null和不等于空
  let sql = 'select * from product where product_out_status ="申请出库" OR product_out_status ="否决" ';
  db.query(sql, (err, results) => {
    if (err) return res.cc(err)
    res.send({
      status: 0,
      message: '获取申请出库产品总数成功',
      total: results.length
    })
  })
}

// 获取出库产品总数
exports.getAllOutProductCount = (req, res) => {
  let sql = 'select * from outproduct';
  db.query(sql, (err, results) => {
    if (err) return res.cc(err)
    res.send({
      status: 0,
      message: '获取出库产品总数成功',
      total: results.length
    })
  })
}

// 获取出库产品列表
exports.getAllOutProductList = (req, res) => {
  let sql = 'select * from outproduct';
  db.query(sql, (err, results) => {
    if (err) return res.cc(err)
    res.send({
      status: 0,
      message: '获取出库产品总数成功',
      results
    })
  })
}

// 监听换页返回的数据

// 入库的分页数据
exports.getInWareProductPageData = (req, res) => {
  let pageIndex = (req.query.pageSize - 1) * 10; // 获取数据起始位置
  let sql = `select * from product where product_out_status="" || isNull(product_out_status) ORDER BY product_create_time limit 10 offset ${pageIndex}`;
  db.query(sql, (err, results) => {
    if (err) return res.cc(err)
    res.send({
      status: 0,
      results
    })
  })
}

// 申请入库的分页数据
exports.getApplyInWareProductPageData = (req, res) => {
  let pageIndex = (req.query.pageSize - 1) * 10; // 获取数据起始位置
  let sql = `select * from product where product_out_status="申请出库" || product_out_status="否决" ORDER BY product_apply_time limit 10 offset ${pageIndex}`;
  db.query(sql, (err, results) => {
    if (err) return res.cc(err)
    res.send({
      status: 0,
      results
    })
  })
}

// 出库的分页数据
exports.getOutInWareProductPageData = (req, res) => {
  let pageIndex = (req.query.pageSize - 1) * 10; // 获取数据起始位置
  let sql = `select * from outproduct ORDER BY product_audit_time limit 10 offset ${pageIndex}`;
  db.query(sql, (err, results) => {
    if (err) return res.cc(err)
    res.send({
      status: 0,
      results
    })
  })
}